/*
 * Config.java
 *
 * 07/11/2009
 */
package kg.sound.player;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author fernando
 */
public class Config {
	
	private static final Logger LOGGER = 
			Logger.getLogger(Config.class.getName());

	private final static File CONFIG_FILE = 
			new File(System.getProperty("user.dir"), "config.properties");

	private final static Properties props = new Properties();

	private Config() {
	}
	
	public static void load() {
		try {
			if (CONFIG_FILE.exists()) {
				props.load(new FileReader(CONFIG_FILE));
				LOGGER.info("Config file load: " + CONFIG_FILE);
			}
		} catch (Throwable th) {
			LOGGER.log(Level.WARNING, "Could not load config file: " + 
					CONFIG_FILE, th);
		}
	}
	
	public static void save() {
		try {
			if (CONFIG_FILE.exists()) {
				CONFIG_FILE.renameTo(new File(CONFIG_FILE, "~"));
			}
			String comments = Main.TITLE + " config file";
			props.store(new FileWriter(CONFIG_FILE), comments);
			LOGGER.info("Config file stored: " + CONFIG_FILE);
		} catch (Throwable th) {
			LOGGER.log(Level.WARNING, "Could not load config file: " + 
					CONFIG_FILE, th);
		}
	}

	public static String getString(String key) {
		return props.getProperty(key);
	}

	public static String getString(String key, String defaultValue) {
		return props.getProperty(key, defaultValue);
	}
	
	public static void setString(String key, String value) {
		props.setProperty(key, value);
	}
}
